/*
 * Copyright 2007-2008  Lukasz Laszko lukaszlaszko@gmail.com
 * 
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for any purpose and without fee is hereby
 * granted, provided that the above copyright notice appear in all
 * copies and that both that the copyright notice and this
 * permission notice and warranty disclaimer appear in supporting
 * documentation, and that the name of the author not be used in
 * advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.
 * 
 * The author disclaim all warranties with regard to this
 * software, including all implied warranties of merchantability
 * and fitness.  In no event shall the author be liable for any
 * special, indirect or consequential damages or any damages
 * whatsoever resulting from loss of use, data or profits, whether
 * in an action of contract, negligence or other tortious action,
 * arising out of or in connection with the use or performance of
 * this software. 
 */
package org.pg.biomedics.asr.mpeg7;

import java.io.StringWriter;
import java.util.List;

import org.simpleframework.xml.Root;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.load.Persister;

/**
 * MPEG-7 SpokenContent description document
 * @author Lukasz Laszko
 */
@Root(name="DescriptionUnit")
public class SpokenContentDocument 
{
    // <editor-fold desc="Fields">
    
    private SpokenContentHeader spokenContentHeader;
    private SpokenContentLattice spokenContentLattice;    
    
    // </editor-fold>
    
    // <editor-fold desc="Properties">
    
    @Element(name="Header")
    public SpokenContentHeader getSpokenContentHeader()
    {
        return spokenContentHeader;
    }    
    
    @Element(name="Header")
    public void setSpokenContentHeader(SpokenContentHeader header)
    {
        this.spokenContentHeader = header;
    }

    public SpokenContentLattice getSpokenContentLattice()
    {
        return spokenContentLattice;
    }

    public void setSpokenContentLattice(SpokenContentLattice lattice)                       
    {
        this.spokenContentLattice = lattice;
    }
    
    /**
     * Gets list of SpokenContentLattice blocks
     * @return list of spoken content lattice blocks
     */
    @ElementList(inline=true)
    public List<Block> getBlocks()
    {
        if (this.spokenContentLattice == null)
            return null;
        else
            return this.spokenContentLattice.getBlocks();
    }
    
    @ElementList(inline=true)
    void setBlocks(List<Block> blocks)
    {
        if (this.spokenContentLattice == null)
            this.spokenContentLattice = new SpokenContentLattice();
        
        this.spokenContentLattice.setBlocks(blocks);
    }
    
    // </editor-fold>
    
    // <editor-fold desc="Methods">
    
    /**
     * Saves document to xml
     * @return xml representation of the document
     */
    public String toXml() throws Exception
    {
        StringWriter writer = new StringWriter();
        writer.write("<Mpeg7 xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"urn:mpeg:mpeg7:schema:2001 Mpeg7-2001.xsd\">");
        
        Serializer serializer = new Persister();
        serializer.write(this, writer);
        
        writer.write("</Mpeg7>");
        
        return writer.toString();
    }
    
    // </editor-fold>
}
